<template>
    <div>
        <van-calendar
            v-model:show="showTimePicker"
            :max-date="maxDate"
            color="#e60044"
            row-height="45"
            confirm-text="确定"
            :show-mark="false"
            :default-date="new Date(defaultDate)"
            :formatter="formatter"
            @confirm="onTimeConfirm"
            @close="onClosed"
        >
            <template #title>
                <div class="fe-title">
                    <h3>日期选择</h3>
                    <img src="@/assets/img/close-popup.png" alt="" @click="onClosed" />
                </div>
            </template>
        </van-calendar>
    </div>
</template>

<script lang="ts" setup>
import { computed} from 'vue';
import { Calendar as VanCalendar, CalendarDayItem} from 'vant';

const props = defineProps({
    isShow: {
        type: Boolean,
        default: false
    },
    defaultDate: {
        type: String,
        default: ''
    },
});

const emit = defineEmits<{(e: 'close'): void;(e: 'confirm', value: Date): void; }>();

const maxDate = new Date('2030-01-01');
const showTimePicker = computed(() => props.isShow);

const formatter = (day: CalendarDayItem)=>{
    const week = day.date?.getDay();
    if(week !== 6 || day.date?.getTime() && day.date?.getTime()<(new Date).getTime()){
        day.type = 'disabled';
    }
    return day;
};

const onTimeConfirm = (date: Date)=>{
    emit('confirm', date);
};

const onClosed = ()=>{
    emit('close');
};
</script>

<style lang="scss" scoped>
.fe-title {
    height: 100%;
    display: flex;
    padding: 0 12px;
    margin-top: 7px;
    align-items: center;

    h3 {
        // font-family: PingFangSC-SNaNpxibold;
        font-weight: 600;
        font-size: 16px;
        color: #333333;
        text-align: center;
        flex: 1;
        margin-left: 20px;
    }

    img {
        width: 20px;
        height: 20px;
    }
}
</style>

